<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <VGroup>
          <el-select class="selectClass" v-model="form.skuRange.type" style="max-width: 150px">
            <el-option v-for="item in skuOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <multi-input v-model="form.skuRange.value" placeholder="SKU" class="w-40"></multi-input>
          <!-- <el-input v-model="form.skuRange.value" placeholder="" prefix-icon="search" clearable class="w-40" /> -->
        </VGroup>
        <el-select v-model="form.platform" placeholder="平台" multiple>
          <el-option v-for="item in options['base.warehouse']?.[0].wmsPlatformType" :key="item.value" :label="item.label"
            :value="item.value"></el-option>
        </el-select>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 150px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()" style="max-width: 300px" />
        </VGroup>

      </template>
      <template #toolbar_btns>
        <el-button type="primary" @click="open('add')"><svg-icon class="text-white mr-1" icon-class="add"/>新增</el-button>
        <el-button @click="handleImport"><svg-icon icon-class="download01" />导入</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" :options="options" @query="query"></ComModal>
    <importFile ref="importRef" type="ware-sku" :api="importSku" @query="query"></importFile>
    <VLog ref="logRef" class="dialogPublic" type="operation" :api="getWareSKULog" options="id"></VLog>
  </VPage>
</template>
<script setup name="BaseWare-sku" lang="jsx">
import { ElMessage, ElMessageBox } from 'element-plus'
import { nextTick, ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryWarSKUList, getWareSKULog, importSku } from './api'
import ComModal from './components/ComModal.vue'
import importFile from '@/components/importFile';
import VLog from '@/components/VLog';
import multiInput from '@/components/multiInput';

const timeOption = [
  { label: "创建时间", value: "create_time" },
  { label: "修改时间", value: "update_time" },
]

const skuOptions = [
  { label: "SKU", value: "sku" },
  { label: "第三方SKU	", value: "fn_sku" },
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 60 },
  formConfig: {
    data: {
      skuRange: { type: 'sku', value: [], range: false },
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    {
      type: "seq", title: '序号', width: 80
    },
    {
      field: 'sku', title: 'SKU', width: 180
    },
    {
      field: 'sku_cn_title', title: '商品名称', width: 220
    },
    {
      field: 'fn_sku', title: '第三方SKU', width: 180
    },
    {
      field: 'platform_name', title: '平台', width: 120
    },
    {
      field: 'status_cn', title: '是否启用', width: 80
    },
    {
      field: 'is_sync_sn', title: '是否同步', width: 80
    },
    {
      field: 'sync_status', title: '同步状态', width: 80
    },
    {
      field: 'fail_reason', title: '备注', width: 160
    },
    {
      field: 'create_user', title: '创建人', width: 160
    },
    {
      field: 'create_time', title: '创建日期', width: 180
    },
    {
      field: 'update_user', title: '修改人', width: 160
    },
    {
      field: 'update_time', title: '修改日期', width: 180
    },
    {
      title: '操作', width: "120", fixed: "right", slots: {
        default: ({ row }) => {
          return <div class="table-option">
            <div>
              <el-link type="primary" underline={false} class="border-right mr-2" onClick={() => open('edit', row.id)}>修改</el-link>
              <el-link type="primary" underline={false} class="border-right" onClick={() => viewLog(row)}>操作记录</el-link>

            </div>
          </div >
        }
      }
    }
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryWarSKUList({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: []
})
const { options } = toRefs(data)
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "base.warehouse"
    ]
  }).then(res => {
    options.value = res.data
  })
}
fetchOption()
provide('options', options)


const ComModalRef = ref()
const open = (type, id) => {
  ComModalRef.value.open(type, id)
}

//查看日志
const logRef = ref()
const viewLog = (id) => {
  logRef.value.open(id)
}

// 导入
const importRef = ref()
const handleImport = () => {
  nextTick(() => {
    importRef.value.open()
  })
}
</script>